package shili;

import java.lang.reflect.Field;

public class ShiLi8 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class clz = Class.forName("reflect.entity.Person");
        Object person = clz.newInstance();

        //获取private String name属性
        Field name = clz.getDeclaredField("name");
        //name属性为private，这里已超出范围，不能直接访问
        //通过setAssessable方法，设定可以访问
        name.setAccessible(true);
        //先取值看一下
        System.out.println("赋值前name: " + name.get(person));
        //为name属性赋值
        name.set(person,"New Person");
        //展示赋值效果
        System.out.println("赋值后的: " + name.get(person));
    }
}
